//
//  FKLookLogisticsVC.m
//  BaiYeMallShop
//
//  Created by Macbook Pro on 2018/11/17.
//  Copyright © 2018 Macbook Pro. All rights reserved.
//

#import "FKLookLogisticsVC.h"
#import "FKLogisticsTopCell.h"
#import "FKLogisticsCell.h"

@interface FKLookLogisticsVC ()

@property (nonatomic, strong) NSMutableDictionary *param;
@property (nonatomic, strong) NSDictionary *data;
@property(nonatomic, assign) NSInteger page;

@end

@implementation FKLookLogisticsVC

- (NSMutableDictionary *)param
{
    if (!_param) {
        _param = [NSMutableDictionary dictionary];
    }
    return _param;
}

//- (NSArray *)datas
//{
//    if (!_datas) {
//        _datas = [NSArray array];
//    }
//    return _datas;
//}

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"查看物流";
    self.param[@"order_id"] = self.orderId;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.data ? 2 : 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    } else {
        NSArray *trace = self.data[@"Traces"];
        return trace.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        FKLogisticsTopCell *cell = [FKLogisticsTopCell cellWith:tableView];
        cell.dict = self.data;
        return cell;
    }
    FKLogisticsCell *cell = [FKLogisticsCell cellWith:tableView];
    NSArray *trace = self.data[@"Traces"];
    cell.dict = trace[indexPath.row];
    if (indexPath.row == 0) {
        cell.type = LogisticsCellTypeUp;
    } else if (indexPath.row == trace.count - 1) {
        cell.type = LogisticsCellTypeDown;
    } else {
        cell.type = LogisticsCellTypeNormal;
    }
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return fk_adjustW(110);
    }
    return fk_adjustW(84);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

#pragma mark-加载数据
-(void)loadData
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"order_id"] = self.orderId;
    [FKHttpTool postWithSuffix:@"m=Home&c=Wuliu&a=logistics_order" param:param success:^(FKBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        if (result.isSuccess) {
            self.data = result.data[@"trace_info"];
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}



@end
